<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

  <script>
    //类
    class Math {
      constructor(value) {
        this.hasInit = true;
        this.value = value;
        if (!value) {
          this.value = 0;
          this.hasInit = false;
        }
      }
      add() {
        let args = [...arguments]
        console.log('我是args', args);
        let initValue = this.hasInit ? this.value : args.shift()
        const value = args.reduce((prev, curv) => prev + curv, initValue)
        return new Math(value)
      }
      minus() {
        let args = [...arguments]
        let initValue = this.hasInit ? this.value : args.shift()
        const value = args.reduce((prev, curv) => prev - curv, initValue)
        return new Math(value)
      }
      mul() {
        let args = [...arguments]
        let initValue = this.hasInit ? this.value : args.shift()
        const value = args.reduce((prev, curv) => prev * curv, initValue)
        return new Math(value)
      }
      divide() {
        let args = [...arguments]
        let initValue = this.hasInit ? this.value : args.shift()
        const value = args.reduce((prev, curv) => prev / (+curv ? curv : 1), initValue)
        return new Math(value)
      }
    }
    let test = new Math()
    const res = test.add(222, 333, 444).minus(333, 222).mul(3, 3).divide(2, 3)
    console.log(res.value)

    // 原型链
    Number.prototype.add = function () {
      let _that = this
      _that = [...arguments].reduce((prev, curv) => prev + curv, _that)
      return _that
    }
    Number.prototype.minus = function () {
      let _that = this
      _that = [...arguments].reduce((prev, curv) => prev - curv, _that)
      return _that
    }
    Number.prototype.mul = function () {
      let _that = this
      _that = [...arguments].reduce((prev, curv) => prev * curv, _that)
      return _that
    }
    Number.prototype.divide = function () {
      let _that = this
      _that = [...arguments].reduce((prev, curv) => prev / (+curv ? curv : 1), _that)
      return _that
    }
    let num = 0;
    let newNum = num.add(222, 333, 444).minus(333, 222).mul(3, 3).divide(2, 3)
    console.log(newNum)
  </script>

  <!-- <script>
    function sum(fn){
      return function xx(...args){
        return args.length===fn.length?fn(...args):(...arg)=>xx(...args,...arg)
      }
    }
    // const curry = (fn) =>(judge = (...args) =>args.length === fn.length? fn(...args): (...arg) => judge(...args, ...arg));
    const add = (a, b, c) => {console.log('abc',a,b,c);return  a + b + c };
    const curryAdd = sum(add);
    console.log('curryAdd',curryAdd);
    console.log(curryAdd(1)(2)(3)); // 6
    // console.log(curryAdd(1, 2)(3)); // 6
    // console.log(curryAdd(1)(2, 3)); // 6
  </script> -->

  <script>
    function sum(fn){
      return function res(...args){
        console.log('args',args);
        return args.length===fn.length?fn(...args):(...arg)=>res(...arg,...args)
      }
    }
    const computed=(a,b,c)=>{ return a+b+c }
    add=sum(computed)
    console.log('add(1)(2)(3)',add(1)(2)(3)); 
  </script>
</body>x

</html>